package com.css.fxfzypg.modules.suggestinfo.analysis.disasterscenario.service.impl;

import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.modules.suggestinfo.analysis.disasterscenario.repository.DisasterScenarioRepository;
import com.css.fxfzypg.modules.suggestinfo.analysis.disasterscenario.repository.entity.DisasterScenarioEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.disasterscenario.service.DisasterScenarioService;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.SysMenuVo;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@Service
public class DisasterScenarioServiceImpl implements DisasterScenarioService {

    @Autowired
    DisasterScenarioRepository disasterScenarioRepository;
    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    SUserService sUserService;

    @Transactional
    @Override
    public Boolean save(DisasterScenarioEntity disasterScenarioEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        disasterScenarioEntity.setId(uuid);
        String divisionFlag = disasterScenarioEntity.getDivisionFlag();
        if(divisionFlag.equals("0")){//按区县
            if(PlatformObjectUtils.isEmpty(disasterScenarioEntity.getProvinceId())||PlatformObjectUtils.isEmpty(disasterScenarioEntity.getCityId())||PlatformObjectUtils.isEmpty(disasterScenarioEntity.getAreaId())){
                String ids = sysAreaService.getIdByCondition(URLEncoder.encode(disasterScenarioEntity.getProvince(),"UTF-8"), URLEncoder.encode(disasterScenarioEntity.getCity(),"UTF-8"), URLEncoder.encode(disasterScenarioEntity.getArea(),"UTF-8"));
                disasterScenarioEntity.setProvinceId(ids.split("_")[0]);
                disasterScenarioEntity.setCityId(ids.split("_")[1]);
                disasterScenarioEntity.setAreaId(ids.split("_")[2]);
            }
        }else if(divisionFlag.equals("1")){//按市
            if(PlatformObjectUtils.isEmpty(disasterScenarioEntity.getProvinceId())||PlatformObjectUtils.isEmpty(disasterScenarioEntity.getCityId())) {
                String provinceId = "";
                String cityId = "";
                List<SysMenuVo> sysMenuList = getSAreaTree("1");//获取所有省
                for (SysMenuVo sysMenuVo : sysMenuList) {
                    String provinceName = sysMenuVo.getName();
                    if (disasterScenarioEntity.getProvince().equals(provinceName)) {
                        provinceId = sysMenuVo.getValue();
                        List<SysMenuVo> citySysMenu = getSAreaTree(provinceId);//获取该省下所有市
                        for (SysMenuVo citySysMenuVo : citySysMenu) {
                            if (citySysMenuVo.getName().equals(disasterScenarioEntity.getCity())) {
                                cityId = citySysMenuVo.getValue();
                            }
                        }
                    }
                }
                disasterScenarioEntity.setProvinceId(provinceId);
                disasterScenarioEntity.setCityId(cityId);
            }
        }
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        disasterScenarioEntity.setCreateId(sUser.getUserId());
        disasterScenarioEntity.setCreateName(sUser.getUserName());
        disasterScenarioEntity.setDelFlag("0");
        disasterScenarioEntity.setCreateTime(new Date());
        return disasterScenarioRepository.save(disasterScenarioEntity);
    }

    private List<SysMenuVo> getSAreaTree(String divisionId) throws UnsupportedEncodingException {
        String jsonStr = sysAreaService.jsonSAreaTree(URLEncoder.encode(divisionId, "UTF-8"));
        Gson gson = CreateGson.createGson();
        Type srItemsStandardType = new TypeToken<List<SysMenuVo>>() {
        }.getType();
        List<SysMenuVo> sysMenuList = gson.fromJson(jsonStr, srItemsStandardType);
        return sysMenuList;
    }

    @Transactional
    @Override
    public Boolean update(DisasterScenarioEntity disasterScenarioEntity){
        return disasterScenarioRepository.update(disasterScenarioEntity);
    }

    @Override
    public DisasterScenarioEntity getDisasterScenarioData(ParamsVo params) {
        return disasterScenarioRepository.getDisasterScenarioData(params);
    }

    @Override
    public List<DisasterScenarioEntity> getDisasterScenarioDataByTaskNum(String taskNum) {
        return disasterScenarioRepository.getDisasterScenarioDataByTaskNum(taskNum);
    }

    @Override
    public DisasterScenarioEntity getDisasterScenarioDataByTaskNum(String taskNum, String cityId, String areaId) {
        return disasterScenarioRepository.getDisasterScenarioDataByTaskNum(taskNum,cityId,areaId);
    }

    @Override
    public List<DisasterScenarioEntity> getDisasterScenarioAreaData(ParamsVo paramsVo) {
        return disasterScenarioRepository.getDisasterScenarioAreaData(paramsVo);
    }

    @Override
    public int getCountById(String id) {
        return disasterScenarioRepository.getCountById(id);
    }
}
